﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CoffeeChat.Client
{
    public class ChatBox : Form
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if ( disposing && ( components != null ) )
            {
                components.Dispose();
            }
            base.Dispose( disposing );
        }
        private void InitializeComponent()
        {
            this.panelChatBox = new System.Windows.Forms.Panel();
            this.label1 = new System.Windows.Forms.Label();
            this.textBoxLog = new System.Windows.Forms.RichTextBox();
            this.textBoxMsg = new System.Windows.Forms.TextBox();
            this.panelMenu = new System.Windows.Forms.Panel();
            this.panelChatBox.SuspendLayout();
            this.SuspendLayout();
            // 
            // panelChatBox
            // 
            this.panelChatBox.Controls.Add(this.label1);
            this.panelChatBox.Controls.Add(this.textBoxLog);
            this.panelChatBox.Controls.Add(this.textBoxMsg);
            this.panelChatBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelChatBox.Location = new System.Drawing.Point(0, 37);
            this.panelChatBox.Name = "panelChatBox";
            this.panelChatBox.Size = new System.Drawing.Size(296, 225);
            this.panelChatBox.TabIndex = 7;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(3, 3);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 9;
            this.label1.Text = "label1";
            // 
            // textBoxLog
            // 
            this.textBoxLog.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBoxLog.Location = new System.Drawing.Point(0, 0);
            this.textBoxLog.Name = "textBoxLog";
            this.textBoxLog.Size = new System.Drawing.Size(296, 205);
            this.textBoxLog.TabIndex = 7;
            this.textBoxLog.Text = "";
            // 
            // textBoxMsg
            // 
            this.textBoxMsg.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.textBoxMsg.Location = new System.Drawing.Point(0, 205);
            this.textBoxMsg.MaxLength = 1023;
            this.textBoxMsg.Name = "textBoxMsg";
            this.textBoxMsg.Size = new System.Drawing.Size(296, 20);
            this.textBoxMsg.TabIndex = 8;
            // 
            // panelMenu
            // 
            this.panelMenu.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelMenu.Location = new System.Drawing.Point(0, 0);
            this.panelMenu.Name = "panelMenu";
            this.panelMenu.Size = new System.Drawing.Size(296, 37);
            this.panelMenu.TabIndex = 8;
            // 
            // ChatBox
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(296, 262);
            this.Controls.Add(this.panelChatBox);
            this.Controls.Add(this.panelMenu);
            this.DoubleBuffered = true;
            this.Name = "ChatBox";
            this.Text = "Chat Box";
            this.panelChatBox.ResumeLayout(false);
            this.panelChatBox.PerformLayout();
            this.ResumeLayout(false);

        }
        

        private string _talkingTo;
        private Panel panelChatBox;
        private Label label1;
        private RichTextBox textBoxLog;
        private TextBox textBoxMsg;
        private Panel panelMenu;
        private string _tempLog;

        public string OtherUser
        {
            get
            {
                return this._talkingTo;
            }
        }

        public ChatBox()
        {
            this.InitializeComponent();

            this.textBoxMsg.KeyUp += this.textBoxMsg_KeyUp;

        }

        private void textBoxMsg_KeyUp(object sender, KeyEventArgs e)
        {
            if ( e.KeyCode == Keys.Enter )
            {
                string msg = textBoxMsg.Text.Trim();
                if ( !string.IsNullOrEmpty( msg ) )
                {
                    textBoxMsg.Clear();

                    Command cmd = new Command();
                    cmd.Intent = 20; //Send Chat Message
                    cmd.Add( "type", "text/plain" );
                    cmd.Add( "to", this._talkingTo );
                    cmd.ExtraData = Command.CurrentEncoding.GetBytes( msg );
                    Program.SendData( cmd );
                }
            }
        }

        public void AddRtf(string rtf)
        {
            if ( this.InvokeRequired )
            {
                this.Invoke( new ChatBoxAddRtfHandler( this.InternalAddRtf ), rtf );
            }
            else
            {
                this.InternalAddRtf( rtf );
            }
        }
        private void InternalAddRtf(string rtf)
        {
            this._tempLog += rtf;

            this.SetRepaint( false );
            this.textBoxLog.Rtf = @"{\rtf" + this._tempLog;
            this.SetRepaint( true );
        }

        public void LoadChat(string userTo)
        {
            if ( this.InvokeRequired )
            {
                this.Invoke( new ChatBoxLoadChatHandler( this.InternalLoadChat ), userTo );
            }
            else
            {
                this.InternalLoadChat( userTo );
            }
        }
        private void InternalLoadChat(string userTo)
        {
            this._talkingTo = userTo;
            this._tempLog += @"You're now talking to \b " + Program.RtfToPlain( userTo ) + @"\b ";
        }


        private IntPtr eventMask;
        private void SetRepaint(bool allow)
        {
            if ( allow )
            {
                NativeMethods.SendMessage( this.textBoxLog.Handle, 1093, IntPtr.Zero, eventMask ); //Turn on events.
                NativeMethods.SendMessage( this.textBoxLog.Handle, 11, new IntPtr( 1 ), IntPtr.Zero ); //Turn on redrawing.
                this.Invalidate( true );
            }
            else
            {
                NativeMethods.SendMessage( this.textBoxLog.Handle, 11, IntPtr.Zero, IntPtr.Zero ); //Stop redrawing.
                eventMask = NativeMethods.SendMessage( this.textBoxLog.Handle, 1083, IntPtr.Zero, IntPtr.Zero ); //Stop sending of events.
            }
        }


    }

    public delegate void ChatBoxAddRtfHandler(string rtf);
    public delegate void ChatBoxLoadChatHandler(string userTo);

}
